package com.lyj.ui.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyj.pojo.Option;
import com.lyj.pojo.Record;
import com.lyj.pojo.Subject;
import com.lyj.pojo.User;
import com.lyj.service.RecordService;
import com.lyj.service.SubjectService;
import com.lyj.service.impl.RecordServiceImpl;
import com.lyj.service.impl.SubjectServiceImpl;
import com.lyj.util.RuleException;

/**
 * Servlet implementation class DoVoteServlet
 */
@WebServlet({"/DoVoteServlet","/m/doVote"})
public class DoVoteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String subjectId = request.getParameter("subjectId");
		User user = (User) request.getSession().getAttribute(User.SESSION_NAME);
		int number = Integer.parseInt(request.getParameter("number"));

		SubjectService subjectService = new SubjectServiceImpl();
		Subject subject = null;
		try {
			subject = subjectService.getSubject(Integer.parseInt(subjectId));
		} catch (NumberFormatException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		Record record = new Record();
		record.setSubjectId(Long.valueOf(subjectId));
		record.setUserId(Long.valueOf(user.getId()));

		RecordService recordService = new RecordServiceImpl();
		
		if(number == 1) {
			String optionsId = request.getParameter("options");
			
			record.setOptionId(Long.valueOf(optionsId));
			
			try {
				recordService.insert(record);
			} catch (RuleException re) {
				request.setAttribute("subject",subject);
				request.setAttribute("message", re.getMessage());
				request.getRequestDispatcher("/WEB-INF/pages/vote.jsp") .forward(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}

			request.setAttribute("subject",subject);
			request.getRequestDispatcher("/WEB-INF/pages/vote.jsp").forward(request, response);
		}
		if(number == 2) {
			String [] optionsId = request.getParameterValues("options");
			for(String optionId:optionsId) {
				try {
					record.setOptionId(Long.valueOf(optionId));	
					recordService.insert(record);
				} catch (RuleException re) {
					request.setAttribute("subject",subject);
					request.setAttribute("message", re.getMessage());
					request.getRequestDispatcher("/WEB-INF/pages/vote.jsp") .forward(request, response);
				}catch (Exception e) {
					e.printStackTrace();
				}			
			}
			request.setAttribute("subject",subject);
			request.getRequestDispatcher("/WEB-INF/pages/vote.jsp").forward(request, response);
		}
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
